package com.ws.service.impl;

import com.github.pagehelper.Page;
import com.ws.common.CommonConstant;
import com.ws.common.PaasPage;
import com.ws.entity.BorrowingRecord;
import com.ws.entity.User;
import com.ws.repository.BorrowingRecordRepository;
import com.ws.service.BorrowingRecordService;
import com.ws.util.PageUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author ：ws
 * @date ：Created in 2023/10/24 10:17
 * @description：TODO
 * @modified By：
 * @version: 1.0.0
 */
@Service
public class BorrowingRecordServiceImpl implements BorrowingRecordService{
    @Autowired
    BorrowingRecordRepository borrowingRecordRepository;

    @Override
    public PaasPage<BorrowingRecord> getBorrowingRecordPage(Pageable pageable, Long readerId,String statusCd) {
        PageUtil.setPageAndOrderBy(pageable);
        BorrowingRecord borrowingRecord = new BorrowingRecord();
        borrowingRecord.setReaderId(readerId);
        if(StringUtils.isEmpty(statusCd)){
            statusCd = CommonConstant.ADD_STATUS_CD;
        }
        borrowingRecord.setStatusCd(statusCd);
        Page<BorrowingRecord> borrowingRecordPage = borrowingRecordRepository.getBorrowingRecordPage(borrowingRecord);
        return PageUtil.transformPage(borrowingRecordPage);
    }

    @Override
    public BorrowingRecord saveBorrowingRecord(BorrowingRecord borrowingRecord) {
        if(null == borrowingRecord.getBorrowingRecordId()){
            borrowingRecord.setStatusCd(CommonConstant.ADD_STATUS_CD);
            borrowingRecord.setCreateTime(new Date());
            borrowingRecordRepository.saveBorrowingRecord(borrowingRecord);
        }else{
            borrowingRecordRepository.updateBorrowingRecord(borrowingRecord);
        }
        return borrowingRecord;
    }

    @Override
    public BorrowingRecord getBorrowingRecordBasicInfo(Long borrowingRecordId) {
        return borrowingRecordRepository.getBorrowingRecordBasicInfo(borrowingRecordId);
    }

    @Override
    public void delBorrowingRecord(Long borrowingRecordId) {
        borrowingRecordRepository.delBorrowingRecord(borrowingRecordId);
    }

    @Override
    public BorrowingRecord updateBorrowingRecord(Long borrowingRecordId, BorrowingRecord borrowingRecord) {
        borrowingRecordRepository.updateBorrowingRecord(borrowingRecord);
        return borrowingRecord;
    }
}
